package com.app.service;

import com.app.model.position.Position;
import com.app.model.position.PositionFlag;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

/**
 * Created by Lichaojie on 2017/4/20. It's a beautiful day.
 */
@Service
public class PositionService {

    public double getDistance(Position p1,Position p2){
        double distance = 0.0;

        double lat1 = (Math.PI/180)*(p1.getLatitude());
        double lon1 = (Math.PI/180)*(p1.getLongitude());
        double lat2 = (Math.PI/180)*(p2.getLatitude());
        double lon2 = (Math.PI/180)*(p2.getLongitude());
        double R = 6371.004;//地球半径，单位KM
        distance += Math.acos(Math.sin(lat1)*Math.sin(lat2)+Math.cos(lat1)*Math.cos(lat2)*Math.cos(lon2-lon1))*R*1000;

        return distance;            //单位：m
    }

    public boolean isNearBy(Position p1,Position p2){
        if(getDistance(p1,p2) - PositionFlag.MAX_DISTANCE < 0){
            return true;
        }else {
            return false;
        }
    }
}
